var ZhuizongAPI = (function($) {
	var pullPage = {start:0,pulling:false};
	var pull;
	var limit = 5;
	var readflag = '';
	
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}

	function baseSetup() {
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		
		// 滑动删除此条
		var btnArray = ['确认', '取消'];
		$('#OA_task_1').on('tap', '.mui-btn', function(event) {
			var elem = this;
			var li = elem.parentNode.parentNode;
			mui.confirm('确认删除该条记录？', '删除此项', btnArray, function(e) {
				if (e.index == 0) {
					li.parentNode.removeChild(li);
				} else {
					setTimeout(function() {
						$.swipeoutClose(li);
					}, 0);
				}
			});
		});
		
		bindUpFreshOnScroll() //绑定上拉加载事件
		
		// 获取活动页签 进行首次数据请求
		// var activeTab = $('#sliderIndicator .mui-indicator.mui-active')[0];
		// var tabIndex = activeTab.getAttribute('data-index')
		// requestData(tabIndex);
		requestData();
		
		// cell的点击事件 去详情页面
		$('.mui-slider-group ul').on('tap', 'a', function(e) {
			var rowid = this.getAttribute('row');
			var flag = this.getAttribute('data-flag');
			if (rowid) {
				var url = 'announceDetail.html';
				var webviewId = 'announceDetail.html';
				webview = plus.webview.create(url, webviewId, {
					titleNView: {
						backgroundColor: '#fff',
						titleText: '通知详情',
						titleColor: '#1D2334',
						autoBackButton: true,
					}
				},{row: rowid,flag: flag});
				webview.show('slide-in-right')
			}else{
				console.log("没有获取到rowid");
			}
		})
	}
	function bindUpFreshOnScroll(){
		pull = $('#scroll0').pullToRefresh({
			up: {
				callback: function() {
					requestData()
				}
			}
		});
		pullPage.pullObj = pull;
	}
	
	function requestData(){
		if (pullPage.pulling && pullPage.pullObj) {
			pullPage.pulling = false //结束刷新
			pullPage.pullObj.endPullUpToRefresh()
			return;
		}
		pullPage.pulling = true //开始刷新了
		var method = 'web.INMPlatform:FindTrackMessageByUser';
		const user = app.getState();
		var arg = {
			parr: readflag,
			input: '',
			APPLoginID: user.loginID,
			APPPerID: user.loginPerID,
			loginID: user.loginID,
			start: pullPage.start,
			limit: limit
		}
		var paramDict = {
			'method': method,
			'params': JSON.stringify(arg),
			'type': 'RecQuery'
		}
		// console.log(JSON.stringify(pullPage));
		plus.nativeUI.showWaiting('正在获取...', {modal: false})
		$.DHCWebService(paramDict, function(result) {
			plus.nativeUI.closeWaiting();
			// console.log(JSON.stringify(result));
			var dataArr = result['data'];
			updateZhuiZongList(dataArr)
		}, function(errorStr) {
			plus.nativeUI.closeWaiting();
		});
	}
	
	// 渲染数据
	function updateZhuiZongList(dataArr){
		var ul = document.getElementById('OA_task_1');
		if(pullPage.start == 0 && dataArr.length == 0){
			// 这里就不再重复显示‘没有更多数据了’
			pullPage.pullObj.options.up.contentnomore = ''
			showNullTip('暂时没有内容，去其他地方看看吧！')
		}else{
			hideNullTip()
		}
		var html = ''
		for (var i=0;i<dataArr.length;i++) {
			var one = dataArr[i];
			var src = one.NoticeFileUrl || 'http://iph.href.lu/400x300?text=没有配图';
			html += '<li class="mui-table-view-cell mui-card"><div class="mui-slider-handle"><div class="mui-card-header">'+ //one.rw +
			'<span class="time">'+ one.date + '</span></div><div class="mui-card-content">'+
			'<div class="mui-card-content-inner">'+one.title+'</div></div>'+
			'<div class="mui-card-footer mui-navigate-right">查看详情</div></div><div class="mui-slider-right mui-disabled">'+
			'<a class="mui-btn mui-btn-red">删除</a></div></li>'
		}
		pullPage.pulling = false;
		pullPage.start += dataArr.length;
		ul.innerHTML += html;
		// console.log(pullPage.start);
		if(dataArr.length >= limit){
			pullPage.pullObj.endPullUpToRefresh()
		}else{
			pullPage.pullObj.endPullUpToRefresh(true)
		}
	}
	
	function showNullTip(msg){
		mui('.nullData')[0].style.display = 'block';
		mui('.nullTip')[0].innerHTML = msg
	}
	
	function hideNullTip(){
		// console.log(index)
		mui('.nullData')[0].style.display = 'none';
	}
	
	// ????
	function refreshMainPage(index) {
		pullPage.start = 0;
		pullPage.pullObj.endPullUpToRefresh(true);
		$('#wrapper').scroll().scrollTo(0,0,100);
		$('#wrapper ul')[0].innerHTML = ''
		pullPage.pullObj.refresh(true);
		requestData()
	}
	
	var AnnouceAPI = {};
	AnnouceAPI.outRefresh = refreshMainPage; // 让外部接口等于内部接口 直接接受入参
	return AnnouceAPI
})(mui);

function refreshData(index){
	// console.log(index)
	AnnouceAPI.outRefresh(index) // true表示重置页面数据，从第0页开始查起
}